package com.changhongit.utils;



import com.alibaba.fastjson.JSONObject;


import com.changhongit.config.IdaasInfo;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;


/**
 * @author 孙宏宇
 * @date 2022/8/8 1:35
 */
public class CloseableHttpClientUtil {


    private static String tokenString = "";
    private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
    private static CloseableHttpClient httpClient = null;


    /**
     * 查询用户列表
     * @return
     */
    public static String doGet() {
        String url="https://chit-admin.cig.tencentcs.com/contacts/api/v1/users";

        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        String token = IdaasInfo.getToken();


        //api_gateway_auth_token自定义header头，用于token验证使用
        httpGet.addHeader("Authorization",token);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询通过用户username模糊查询用户信息
     * @param url
     * @param username
     * @return
     * @throws URISyntaxException
     */
    public static String doGetbyParams(String url,String username) throws URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URIBuilder uriBuilder = new URIBuilder(url).setParameter("keywords",username);
        uriBuilder.addParameter("type","USER_NAME");
        System.out.println("urlB-==========="+uriBuilder);
        HttpGet httpGet = new HttpGet(String.valueOf(uriBuilder));
        String token = IdaasInfo.getToken();


        //api_gateway_auth_token自定义header头，用于token验证使用
        httpGet.addHeader("Authorization",token);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {

            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 基于用户ID查询用户信息
     * @param url
     * @param path
     * @return
     * @throws URISyntaxException
     */
    public static String doGetbyPath(String url,String path) throws URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URIBuilder uriBuilder = new URIBuilder(url).setPath("contacts/api/v1/users/"+path);
        System.out.println("urlB-==========="+uriBuilder);
        HttpGet httpGet = new HttpGet(String.valueOf(uriBuilder));
        String token = IdaasInfo.getToken();


        //api_gateway_auth_token自定义header头，用于token验证使用
        httpGet.addHeader("Authorization",token);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {

            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加用户
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url,JSONObject json) {
        if (null == httpClient) {
            httpClient = HttpClientBuilder.create().build();
        }
        HttpPost httpPost = new HttpPost(url);

        String token = IdaasInfo.getToken();
        //api_gateway_auth_token自定义header头，用于token验证使用
        httpPost.addHeader("Authorization", token);
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            //发送json数据需要设置contentType
            se.setContentType("application/json");
            //设置请求参数
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 基于用户ID删除用户
     * @param url
     * @param path
     * @return
     * @throws URISyntaxException
     */

    public static String idaasurl="https://chit-admin.cig.tencentcs.com/contacts/api/v1/users";

    public static String doDelbyPath(String url,String path) throws URISyntaxException {
        //创建HttpClient对象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        URIBuilder uriBuilder = new URIBuilder(url).setPath("contacts/api/v1/users/"+path);
        System.out.println("urlB-==========="+uriBuilder);
        HttpDelete httpDelete = new HttpDelete(String.valueOf(uriBuilder));
        String token = IdaasInfo.getToken();


        //api_gateway_auth_token自定义header头，用于token验证使用
        httpDelete.addHeader("Authorization",token);
        httpDelete.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {

            HttpResponse response = httpClient.execute(httpDelete);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 测试
     */
    public static void test() throws URISyntaxException {
        String url="https://chit-admin.cig.tencentcs.com/contacts/api/v1/users";

        /**
         * 创建用户
         */
        //
    //        JSONObject zhangsan = new JSONObject();
    //        try {
    //            //添加
    //            zhangsan.put("username", "zhangsan");
    //            zhangsan.put("displayName", "zhangsan");
    //            zhangsan.put("phoneNum", "13808806661");
    //            zhangsan.put("deptId", "dpf-146804f72f0e481196853ef0bfc5e345");
    //            zhangsan.put("primaryMail", "zhangsan@changhongit.com");
    //            System.out.println(zhangsan.toString());
    //        } catch (JSONException e) {
    //            e.printStackTrace();
    //        }
    //
    //        JSONObject object = new JSONObject();
    //        object.put("values",zhangsan);
    //        String post = doPost(url, object);
    //        System.out.println(post);

    //
    //String rep = doGet(url);
    // System.out.println(rep);
          //  String s = doGet(url);
           // System.out.println(s);

     //String zhangsan = doGetbyParams(url, "zhangsan");
       // System.out.println(zhangsan);

//        String zhangsan = doDelbyPath(url, "us-d13c60b50aef436280e9dd373febabd5");
//        System.out.println(zhangsan);
//        String s = doGet(url);
//        System.out.println(s);



    }


    public static void main(String[] args) throws URISyntaxException {
        test();

    }


}
